// 修改用户信息
const router = require("koa-router")();
/* 查询所有的普通用户*/
import { ReadFile, WriteFile } from '../utils/ReadWriteFile'
/* 登出 */
router.post("/api/resetPassword", async ctx => {
      const { username, password, newPassword, type } = ctx.request.body;
      console.log(type)
      try {
            let result = await ReadFile(type);
            // 检查用户名是否已在数据库
            let res = result.find(item => item.username == username && item.password == password );
            if (res) {
                //  存在该用户，修改密码
                  res.password = newPassword;
                  WriteFile({ type, result })
                  // await jsonfile.writeFile(process.cwd() + `/data/${type}.json`, result);
                  ctx.body = {
                        code: 200,
                        msg: "修改密码成功"
                  }
            } else {
                  ctx.body = {
                        code: 400,
                        msg: "该用户不存在或旧密码错误,修改密码失败"
                  }
            }
      } catch (err) {
            ctx.body = {
                  msg: "请求参数不合法",
                  code: 400
            }
      }

})
module.exports = router;




